#!/usr/bin/env python3

import sys
sys.path += ["usr/lib/epm"]

from optparse import OptionParser
from epmaux import *
import settings

def main():
    # global options
    # global verbose
    
    cmd = ""
    if len(sys.argv) >= 2:
        cmd = sys.argv[1]
    else:
        print("Type 'epm help' for usage.")
        return

    if cmd == "run" or cmd == "r":
        run_project(sys.argv[2:])
        return
    
    parser = OptionParser()
    parser.add_option("-t", "--type",
                      action="store", type="string", dest="type", default="exe",
                      help="output type")
    parser.add_option("-v", "--verbose",
                      action="store_true", dest="verbose", default=False,
                      help="turn on verbose mode")

    (options, args) = parser.parse_args(sys.argv[2:])

    settings.verbose = options.verbose
    #print settings.verbose
    # print verbose


    if cmd == "buildsolution" or cmd == "buildsln" or cmd == "bs":
        build_solution()
    elif cmd == "buildproject" or cmd == "buildprj" or cmd == "bp" or cmd == "build" or cmd == "b":
        return build_project()
    elif cmd == "compile" or cmd == "c":
        return compile_sourcefile(args[0])
    elif cmd == "add":
        add_source(args)
    elif cmd == "delete" or cmd == "del" or cmd == "remove" or cmd == "rm":
        remove_source(args)
    elif cmd == "np" or cmd == "newproject" or cmd == "newprj" or cmd == "new":
        new_project(args[0], options.type)
    elif cmd == "ns" or cmd == "newsolution" or cmd == "newsln":
        new_solution(args[0])
    elif cmd == "ap" or cmd == "addp" or cmd == "addprj" or cmd == "addproject":
        add_project(args[0])
    elif cmd == "rp" or cmd == "rmp" or cmd == "rmprj" or cmd == "rmproject" or cmd == "removep" or cmd == "removeprj" or cmd == "removeproject":
        remove_project(args[0])
    elif cmd == "addconfig":
        add_config(args[0])
    elif cmd == "setactiveconfig":
        set_active_config(args[0])
    elif cmd == "buildgch" or cmd == "pch" or cmd == "gch":
        build_gch()
    elif cmd == "addpch":
        add_pch(args)
    elif cmd == "rmpch" or cmd == "delpch" or cmd == "removepch":
        remove_pch(args)
    elif cmd == "gui":
        open_gui()
    elif cmd == "help" or cmd == "-h" or cmd == "--help":
        print("usage: epm <subcommand> [options] [args]")
        print("Available subcommands:")
        print("   newproject (newprj, new)")
        print("   add")
        print("   buildproject (buildprj, bp, build, b)")
    else:
        print("Unknown command: '" + cmd + "'")

if __name__ == "__main__":
    sys.exit(main())
